{% extends "base.html" %}

{% block extra_css %}
<style>
    .stat-card {
        transition: transform 0.2s;
    }
    .stat-card:hover {
        transform: translateY(-5px);
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- 统计卡片 -->
    <div class="row mb-4">
        <div class="col-md-6 col-lg-3 mb-3">
            <div class="card stat-card">
                <div class="card-body">
                    <h6 class="card-title text-muted">设备总数</h6>
                    <h2 class="card-text">{{ recent_devices|length }}</h2>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-lg-3 mb-3">
            <div class="card stat-card">
                <div class="card-body">
                    <h6 class="card-title text-muted">在线比例</h6>
                    <h2 class="card-text">{{ "%.1f"|format(online_ratio) }}%</h2>
                </div>
            </div>
        </div>
    </div>

    <!-- 设备类型统计 -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-chart-pie me-2"></i>设备类型分布</h5>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>设备类型</th>
                                <th>数量</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for stat in device_type_stats %}
                            <tr>
                                <td>
                                    {% if stat.device_type == 'sensor' %}传感器
                                    {% elif stat.device_type == 'pump' %}水泵
                                    {% elif stat.device_type == 'valve' %}阀门
                                    {% elif stat.device_type == 'meter' %}流量计
                                    {% else %}其他
                                    {% endif %}
                                </td>
                                <td>{{ stat.count }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-signal me-2"></i>设备状态分布</h5>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>状态</th>
                                <th>数量</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for stat in status_stats %}
                            <tr>
                                <td>{{ '在线' if stat.status == 'online' else '离线' }}</td>
                                <td>{{ stat.count }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- 最近添加的设备 -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-clock me-2"></i>最近添加的设备</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>设备ID</th>
                                    <th>名称</th>
                                    <th>类型</th>
                                    <th>位置</th>
                                    <th>状态</th>
                                    <th>添加时间</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for device in recent_devices %}
                                <tr>
                                    <td>{{ device.device_id }}</td>
                                    <td>{{ device.name }}</td>
                                    <td>
                                        {% if device.device_type == 'sensor' %}传感器
                                        {% elif device.device_type == 'pump' %}水泵
                                        {% elif device.device_type == 'valve' %}阀门
                                        {% elif device.device_type == 'meter' %}流量计
                                        {% else %}其他
                                        {% endif %}
                                    </td>
                                    <td>{{ device.location }}</td>
                                    <td>
                                        <span class="badge {% if device.status == 'online' %}bg-success{% else %}bg-danger{% endif %}">
                                            {{ '在线' if device.status == 'online' else '离线' }}
                                        </span>
                                    </td>
                                    <td>{{ device.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 